package com.example.seabedmusic.views;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.seabedmusic.R;
import com.example.seabedmusic.adapter.NotificationSettingsAdapter;
import com.example.seabedmusic.entity.NotificationSettingItem;
import java.util.ArrayList;
import java.util.List;

public class TeenModeActivity extends AppCompatActivity implements NotificationSettingsAdapter.OnSwitchChangeListener {

    private RecyclerView rvTeenMode;
    private NotificationSettingsAdapter adapter;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_list);

        preferences = getSharedPreferences("teen_mode_settings", MODE_PRIVATE);
        initViews();
        setupTeenModeList();
    }

    private void initViews() {
        rvTeenMode = findViewById(R.id.rvSettings);
        rvTeenMode.setLayoutManager(new LinearLayoutManager(this));
        setTitle("未成年模式");
    }

    private void setupTeenModeList() {
        List<NotificationSettingItem> items = new ArrayList<>();

        boolean teenModeEnabled = preferences.getBoolean("teen_mode_enabled", false);

        items.add(new NotificationSettingItem("未成年模式", "开启后限制使用时间和内容", teenModeEnabled));
        items.add(new NotificationSettingItem("使用时间限制", "每日最多使用2小时",
                preferences.getBoolean("time_limit", true)));
        items.add(new NotificationSettingItem("夜间禁用", "22:00-8:00禁止使用",
                preferences.getBoolean("night_ban", true)));
        items.add(new NotificationSettingItem("消费限制", "禁止充值消费",
                preferences.getBoolean("purchase_limit", true)));
        items.add(new NotificationSettingItem("内容过滤", "过滤不适合内容",
                preferences.getBoolean("content_filter", true)));
        items.add(new NotificationSettingItem("隐私保护", "加强隐私保护设置",
                preferences.getBoolean("privacy_protection", true)));

        adapter = new NotificationSettingsAdapter(items);
        adapter.setOnSwitchChangeListener(this);
        rvTeenMode.setAdapter(adapter);

        // 根据未成年模式总开关设置其他项的可用性
        adapter.setItemsEnabled(teenModeEnabled);
    }

    @Override
    public void onSwitchChanged(int position, boolean isChecked) {
        NotificationSettingItem item = adapter.getItemAtPosition(position);
        if (item != null) {
            switch (item.getTitle()) {
                case "未成年模式":
                    if (isChecked) {
                        showTeenModePasswordDialog();
                    } else {
                        disableTeenMode();
                    }
                    break;
                case "使用时间限制":
                    preferences.edit().putBoolean("time_limit", isChecked).apply();
                    Toast.makeText(this, isChecked ? "已开启使用时间限制" : "已关闭使用时间限制", Toast.LENGTH_SHORT).show();
                    break;
                case "夜间禁用":
                    preferences.edit().putBoolean("night_ban", isChecked).apply();
                    Toast.makeText(this, isChecked ? "已开启夜间禁用" : "已关闭夜间禁用", Toast.LENGTH_SHORT).show();
                    break;
                case "消费限制":
                    preferences.edit().putBoolean("purchase_limit", isChecked).apply();
                    Toast.makeText(this, isChecked ? "已开启消费限制" : "已关闭消费限制", Toast.LENGTH_SHORT).show();
                    break;
                case "内容过滤":
                    preferences.edit().putBoolean("content_filter", isChecked).apply();
                    Toast.makeText(this, isChecked ? "已开启内容过滤" : "已关闭内容过滤", Toast.LENGTH_SHORT).show();
                    break;
                case "隐私保护":
                    preferences.edit().putBoolean("privacy_protection", isChecked).apply();
                    Toast.makeText(this, isChecked ? "已加强隐私保护" : "已恢复普通隐私设置", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    private void showTeenModePasswordDialog() {
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
        builder.setTitle("开启未成年模式");
        builder.setMessage("请设置家长密码（4位数字）");

        android.widget.LinearLayout layout = new android.widget.LinearLayout(this);
        layout.setOrientation(android.widget.LinearLayout.VERTICAL);
        layout.setPadding(50, 40, 50, 10);

        final android.widget.EditText etPassword = new android.widget.EditText(this);
        etPassword.setHint("请输入4位数字密码");
        etPassword.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
        etPassword.setMaxEms(4);

        layout.addView(etPassword);
        builder.setView(layout);

        builder.setPositiveButton("确定", (dialog, which) -> {
            String password = etPassword.getText().toString();
            if (password.length() == 4) {
                enableTeenMode(password);
            } else {
                Toast.makeText(this, "密码必须为4位数字", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("取消", (dialog, which) -> {
            adapter.setItemChecked(0, false);
        });

        builder.show();
    }

    private void enableTeenMode(String password) {
        preferences.edit()
                .putBoolean("teen_mode_enabled", true)
                .putString("teen_mode_password", password)
                .apply();

        adapter.setItemsEnabled(true);
        Toast.makeText(this, "未成年模式已开启", Toast.LENGTH_SHORT).show();
    }

    private void disableTeenMode() {
        // 需要验证密码才能关闭
        showDisablePasswordDialog();
    }

    private void showDisablePasswordDialog() {
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
        builder.setTitle("关闭未成年模式");
        builder.setMessage("请输入家长密码");

        android.widget.LinearLayout layout = new android.widget.LinearLayout(this);
        layout.setOrientation(android.widget.LinearLayout.VERTICAL);
        layout.setPadding(50, 40, 50, 10);

        final android.widget.EditText etPassword = new android.widget.EditText(this);
        etPassword.setHint("请输入4位数字密码");
        etPassword.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
        etPassword.setMaxEms(4);

        layout.addView(etPassword);
        builder.setView(layout);

        builder.setPositiveButton("确定", (dialog, which) -> {
            String inputPassword = etPassword.getText().toString();
            String savedPassword = preferences.getString("teen_mode_password", "");

            if (inputPassword.equals(savedPassword)) {
                preferences.edit()
                        .putBoolean("teen_mode_enabled", false)
                        .apply();
                adapter.setItemsEnabled(false);
                Toast.makeText(this, "未成年模式已关闭", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
                adapter.setItemChecked(0, true);
            }
        });

        builder.setNegativeButton("取消", (dialog, which) -> {
            adapter.setItemChecked(0, true);
        });

        builder.show();
    }
}